﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using System.IO;

using LuaInterface;

namespace CodeGame
{
    class ScriptingManager
    {
        private Lua LuaEngine;

        public ScriptingManager()
        {
            this.LuaEngine = new Lua();

            this.LoadAssembly(Assembly.GetCallingAssembly(), "CodeGame.LuaLibary");
            this.LoadLuaSideLibary(true);
        }

        public void LoadLuaSideLibary(bool firstLoad)
        {
            foreach (string item in Directory.GetFiles("lib", "*.lua"))
            {
                try
                {
                    if (!firstLoad)
                    {
                        Singletons.Console.Print("Loading Libary: " + item, Singletons.NormalText);
                    }

                    this.LuaEngine.DoFile(item);
                }
                catch (Exception ex)
                {
                    if (!firstLoad)
                    {
                        Singletons.Console.Print("Error in loading " + item + " : " + ex.Message, Singletons.ErrorText);
                    }

                    Console.WriteLine("Error in loading " + item + " : " + ex.Message);
                }
            }
        }

        public void LoadAssembly(Assembly asm, string space)
        {
            foreach (Type item in asm.GetTypes())
            {
                Console.WriteLine(item.Namespace + " : " + item.IsAbstract.ToString() + " : " + item.IsSealed.ToString());
                if (item.Namespace == space && item.IsAbstract && item.IsSealed)
                {
                    Console.WriteLine("LOADING : " + item.FullName);
                    string tablename = item.Name.Split('_')[1];

                    if (tablename != "global")
                    {
                        this.LuaEngine.NewTable(tablename);
                    }

                    foreach (MethodInfo item2 in item.GetMethods(BindingFlags.Public | BindingFlags.Static))
                    {
                        if (tablename == "global")
                        {
                            this.LuaEngine.RegisterFunction(item2.Name, null, item2);
                        }
                        else
                        {
                            this.LuaEngine.RegisterFunction(tablename + "." + item2.Name, null, item2);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Not Loading: " + item.FullName);
                }
            }
        }

        public void DoString(string line)
        {
            this.LuaEngine.DoString(line);
        }

        public void DoFile(string filename)
        {
            this.LuaEngine.DoFile(filename);
        }

        public object GetGlobal(string global)
        {
            return this.LuaEngine[global];
        }

        public void SetGlobal(string global, object value)
        {
            this.LuaEngine[global] = value;
        }

        public Lua GetRaw()
        {
            return this.LuaEngine;
        }
    }
}
